package com.gticket.customerbackend.handler;

import com.gticket.customerbackend.pojo.Level;
import com.gticket.customerbackend.pojo.ResponseDate;
import com.gticket.customerbackend.service.LevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * vip等级level的控制层
 */
@RestController
@Data
@Api(tags = "vip信息及设置")
@RequestMapping("/levels")
public class LevelHandler {
    @Autowired
    private LevelService levelService;
    /**
     * 获取所有vip等级信息
     * @return
     */
    @ApiOperation(value = "查询所有level",notes = "")
//    @Cacheable(value = "listLevels",keyGenerator = "myKeyGenerator")
    @GetMapping("")
    public ResponseDate listLevel(){
        ResponseDate responseDate=new ResponseDate();
        Level level=new Level();
        try {
            List<Level> levels=levelService.listLevels(level);
            responseDate.setCode(200);
            responseDate.setMsg("查询成功");
            responseDate.setData(levels);
        }catch (Exception e){
            responseDate.setCode(500);
            responseDate.setMsg("接口异常");
        }
        return responseDate;
    }

    /**
     * 添加vip等级
     * @param level
     */
    @ApiOperation(value = "添加level",notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level_name",value = "vip4",required = true),
            @ApiImplicitParam(name = "min_score",value = "1000",required = true),
            @ApiImplicitParam(name = "max_score",value = "2000",required = true),
            @ApiImplicitParam(name = "discount",value = "0.8",required = true)
    })
//    @Cacheable(value = "insertLevel",keyGenerator = "myKeyGenerator")
    @PostMapping
    public ResponseDate insertLevel(Level level){
        ResponseDate responseDate=new ResponseDate();
        try {
            levelService.insertLevel(level);
            responseDate.setCode(200);
            responseDate.setMsg("添加成功");
        }catch (Exception e){
            responseDate.setCode(500);
            responseDate.setMsg("接口异常");
        }
        return responseDate;

    }


    @ApiOperation(value = "删除level",notes = "利用软删除，改变flag状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level_id",value = "1",required = true)
    })
    @DeleteMapping
//    @CacheEvict(value = "listLevels" ,allEntries = true)
    public ResponseDate deleteLevelById(Level level){
        ResponseDate responseDate=new ResponseDate();
        try {
            levelService.deleteLevel(level);
            responseDate.setCode(200);
            responseDate.setMsg("删除成功");
        }catch (Exception e){
            responseDate.setCode(500);
            responseDate.setMsg("接口异常");
        }
        return responseDate;
    }

    @ApiOperation(value = "修改level",notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level_id",value = "1",required = true),
            @ApiImplicitParam(name = "level_name",value = "vip4",required = true),
            @ApiImplicitParam(name = "min_score",value = "1000",required = true),
            @ApiImplicitParam(name = "max_score",value = "2000",required = true),
            @ApiImplicitParam(name = "discount",value = "0.8",required = true)
    })
//    @CacheEvict(value = "listLevels" ,allEntries = true)
    @PutMapping
    public ResponseDate updateLevel(Level level){
        ResponseDate responseDate=new ResponseDate();
        try {
            levelService.updateLevel(level);
            responseDate.setCode(200);
            responseDate.setMsg("修改成功");
        }catch (Exception e){
            responseDate.setCode(500);
            responseDate.setMsg("接口异常");
        }
        return responseDate;

    }

}
